/**
 * BlackPlayer represents the black player in a game of chess.
 * 
 * @author Brian Boadi, Antoine Domenger
 * @version 1.0
 */

package lp.chess;

public class BlackPlayer extends Player {
	//- CONSTRUCTOR(S) ------------------------------------------------------------------------

	/**
 	 * Default constructor.
 	 * Initializes a black player with all of its pieces.
 	 */
	public BlackPlayer()
	{
		m_pieces = new Piece[16];
		
		// First row :
		m_pieces[0] = new Rook(1, 1, false);
		m_pieces[1] = new Knight(2, 1, false);
		m_pieces[2] = new Bishop(3, 1, false);
		m_pieces[3] = new King(4, 1, false);
		m_pieces[4] = new Queen(5, 1, false);
		m_pieces[5] = new Bishop(6, 1, false);
		m_pieces[6] = new Knight(7, 1, false);
		m_pieces[7] = new Rook(8, 1, false);
		
		// Second row :
		m_pieces[8] = new Pawn(1, 2, false);
		m_pieces[9] = new Pawn(2, 2, false);
		m_pieces[10] = new Pawn(3, 2, false);
		m_pieces[11] = new Pawn(4, 2, false);
		m_pieces[12] = new Pawn(5, 2, false);
		m_pieces[13] = new Pawn(6, 2, false);
		m_pieces[14] = new Pawn(7, 2, false);
		m_pieces[15] = new Pawn(8, 2, false);
	}
	
	/**
 	 * Copy constructor.
 	 * Initializes a black player with all of its pieces, given another one.
 	 * 
 	 * @param other the other player to copy in this one.
 	 */
	public BlackPlayer(BlackPlayer other) {
		m_pieces = new Piece[16];
		
		// First row :
		m_pieces[0] = new Rook((Rook)other.getPieces()[0]);
		m_pieces[1] = new Knight((Knight)other.getPieces()[1]);
		m_pieces[2] = new Bishop((Bishop)other.getPieces()[2]);
		m_pieces[3] = new King((King)other.getPieces()[3]);
		m_pieces[4] = new Queen((Queen)other.getPieces()[4]);
		m_pieces[5] = new Bishop((Bishop)other.getPieces()[5]);
		m_pieces[6] = new Knight((Knight)other.getPieces()[6]);
		m_pieces[7] = new Rook((Rook)other.getPieces()[7]);
		
		// Second row :
		m_pieces[8] = new Pawn((Pawn)other.getPieces()[8]);
		m_pieces[9] = new Pawn((Pawn)other.getPieces()[9]);
		m_pieces[10] = new Pawn((Pawn)other.getPieces()[10]);
		m_pieces[11] = new Pawn((Pawn)other.getPieces()[11]);
		m_pieces[12] = new Pawn((Pawn)other.getPieces()[12]);
		m_pieces[13] = new Pawn((Pawn)other.getPieces()[13]);
		m_pieces[14] = new Pawn((Pawn)other.getPieces()[14]);
		m_pieces[15] = new Pawn((Pawn)other.getPieces()[15]);
	}

	//-----------------------------------------------------------------------------------------

	//- OTHER METHOD(S) -----------------------------------------------------------------------
	
	/**
	 * Gets the pieces owned by this black player.
	 * 
	 * @return the array of all the pieces owned by this black player.
	 */
	public Piece[] getPieces() {
		return m_pieces;
	}
	
	/**
	 * Returns true if this black player is in check.
	 * 
	 * @param cb the chessboard.
	 * @return true if this black player is in check.
	 */
	public boolean isInCheck(Chessboard cb) {
		int i = 0;
		int xk, yk;
		int whitePiecesTested = 0;
		boolean check = false;
		
		// First we retrieve the black player's king and it's coordinates:
		Piece bk = m_pieces[3];
		xk = bk.getX();
		yk = bk.getY();
		
		// Then we search the chessboard for a piece that could take over the king during the next turn:
		while(i < 64 && whitePiecesTested < 16 && !check) {	// The searching will stop if we have reached the last case or if every white piece has been checked.
			if(cb.getCases()[i] != null && cb.getCases()[i].isWhite()) {	// There is a piece on this case and it's a white one:
				if(cb.getCases()[i].validMove(cb, xk, yk, false)) {
					check = true;
				}
				whitePiecesTested++;
			}
			
			i++;
		}
		
		return check;
	}
	
	//-----------------------------------------------------------------------------------------
	
	// DISPLAY METHOD(S) ----------------------------------------------------------------------

	/**
	 * Displays the black player with all of its pieces.
	 */
	public void display()
	{
		for(int i=0 ; i<16 ; i++)
		{
			System.out.println("Piece " + i+1 + " : ");
			m_pieces[i].display();
		}
	}

	//-----------------------------------------------------------------------------------------
}
